/** 
* Selection wrapper for crossbrowser usage. Loads a selection object from a window object.
* @requires Kwik.BrowserInfo
* @requires Kwik.Object
* @extends Kwik.Object
* @constructor
* @created 3:36 PM 1/31/2009
* @modified 3:36 PM 1/31/2009
*/
Kwik.Dom.Selection = function(win)
{
	/** @ignore */
	Kwik.Utility.extend(this, "Kwik.Object");
	this.__class.push("Kwik.Dom.Selection");

	/** Selection type, 'text' or 'control' @type String */
	this.type = null;
	/** Selected element @type HTMLElement */
	this.element = null;
	/** Reference to a window object of which the selection is active @type Window */
	this._window = null;
	/** Reference to the original selection */
	this._selection = null;
	
	/** Load object with specified window */
	this.load = function(win)
	{
		this._window = win;
		if(Kwik.BrowserInfo.internetExplorer)
			this._loadExplorer();
		else
			this._loadGecko();
	};

	/** Reload the values */
	this.reload = function()
	{
		this.load(this._window);
	};

	/** Internal method for loading from Internet Explorer browsers */
	this._loadExplorer = function()
	{
		Kwik.Page.log.write("Kwik.Dom.Selection.loadExplorer: Starting explorer loading.");
		this.element = null;
		this.type = null;
		this.selection = null;
		var range = null;

		try
		{
			this.selection = this._window.document.selection;
			range = this.selection.createRange();
		}
		catch(x)
		{
			Kwik.Page.log.write("Kwik.Dom.Selection.loadExplorer: Failed to create range from selection.\r\n" + x.description);
			return;
		}
		if(this.selection)
		{
			this.type = this.selection.type.toLowerCase();
			Kwik.Page.log.write("Kwik.Dom.Selection.loadExplorer: Selection set, type determined '" + this.type + "'.");
			
			if(this.type == "control" && range && range.item)
			{
				Kwik.Page.log.write("Kwik.Dom.Selection.loadExporer: Type 'control using range.item(0) for element.");
				this.element = range.item(0);
			}
			else
			{
				Kwik.Page.log.write("Kwik.Dom.Selection.loadExplorer: Type 'text' using parentElement() for element.");
				this.element = range.parentElement();
			}
		}
	};

	/** Internal method for loading from Gecko browsers */
	this._loadGecko = function()
	{
		Kwik.Page.log.write("Kwik.Dom.Selection.loadGecko: Starting gecko loading.");
		this.selection = this._window.getSelection();
		this.type = null;
		this.element = null;
		if(this.selection && this.selection.rangeCount == 1)
		{
			Kwik.Page.log.write("Kwik.Dom.Selection.loadGecko: Selection and rangeCount.");
			//set type and element by first range
			var range = this.selection.getRangeAt(0);
			if(range.startContainer == range.endContainer && (range.endOffset - range.startOffset) == 1)
			{
				Kwik.Page.log.write("Kwik.Dom.Selection.loadGecko: Type 'control' determined, getting element from anchorNode.");
				this.type = "control";
				this.element = this.selection.anchorNode.childNodes[this.selection.anchorOffset];
			}
			else
			{
				Kwik.Page.log.write("Kwik.Dom.Selection.loadGecko: Type 'text' determined getting parent from anchorNode.");
				this.type = "text";
				var node = this.selection.anchorNode;
				while(node && node.nodeType != 1){node = node.parentNode;}
				this.element = node;
			}
		}
	};

	/** @ignore */
	this.main = function(win)
	{
		if(win){this.load(win);}
	}; this.main(win);
};